package com.yuuzy.qqclient.service;

import com.yuuzy.qqcommon.Message;
import com.yuuzy.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 该类提供和消息相关的服务方法
 */
public class MessageClientService {
	/**
	 *
	 * @param content 内容
	 * @param senderID 发送者ID
	 * @param getterID 接受者ID
	 */
	public static void sendMessageToOne(String content,String senderID,String getterID) throws IOException {
		//构建message
		Message message = new Message();
		message.setSender(senderID);
		message.setGetter(getterID);
		message.setContent(content);
		message.setMesType(MessageType.MESSAGE_COMM_MES);
		message.setSendTime(new Date().toString());//发送时间设置到message对象
		System.out.println("你对" + getterID + "说:" + content);
		//发送给服务端
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.
				getClientConnectserverThread(senderID).getSocket().getOutputStream());
		objectOutputStream.writeObject(message);
	}

	public static void sendMessageToAll(String content,String senderID) throws IOException {
		//构建message
		Message message = new Message();
		message.setSender(senderID);
		message.setGetter(senderID);//为了避免判断，实际上无意义
		message.setContent(content);
		message.setMesType(MessageType.MESSAGE_TO_ALL_MES);
		message.setSendTime(new Date().toString());//发送时间设置到message对象
		System.out.println("你对大家说:" + content);
		//发送给服务端
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.
				getClientConnectserverThread(senderID).getSocket().getOutputStream());
		objectOutputStream.writeObject(message);
	}
}
